
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// Sys_Currency Data Class
    /// </summary>
    public partial class Sys_CurrencyDAL : BaseDAL
    {
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Sys_Currency";

        public Sys_CurrencyDAL()
        {}

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Sys_Currency model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("insert into [Sys_Currency](");
            strSql.Append("[CurrencyID],[CurrencyName],[Rate],[Memo],[Creator],[CreateTime],[LastModifier],[LastModifyTime]");
            strSql.Append(") values (");
            strSql.Append("@CurrencyID,@CurrencyName,@Rate,@Memo,@Creator,@CreateTime,@LastModifier,@LastModifyTime");
            strSql.Append(")");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@CurrencyID", model.CurrencyID),
                    new SqlParameter("@CurrencyName", model.CurrencyName),
                    new SqlParameter("@Rate", model.Rate),
                    new SqlParameter("@Memo", model.Memo),
                    new SqlParameter("@Creator", model.Creator),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@LastModifier", model.LastModifier),
                    new SqlParameter("@LastModifyTime", model.LastModifyTime)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Sys_Currency model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("update [Sys_Currency] set ");
            strSql.Append("[CurrencyName]=@CurrencyName,[Rate]=@Rate,[Memo]=@Memo,[Creator]=@Creator,[CreateTime]=@CreateTime,[LastModifier]=@LastModifier,[LastModifyTime]=@LastModifyTime");
            strSql.Append(" where [CurrencyID]=@CurrencyID");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@CurrencyID", model.CurrencyID),
                    new SqlParameter("@CurrencyName", model.CurrencyName),
                    new SqlParameter("@Rate", model.Rate),
                    new SqlParameter("@Memo", model.Memo),
                    new SqlParameter("@Creator", model.Creator),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@LastModifier", model.LastModifier),
                    new SqlParameter("@LastModifyTime", model.LastModifyTime)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(String CurrencyID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Sys_Currency] ");
            strSql.Append(" where [CurrencyID]=@CurrencyID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("@CurrencyID", CurrencyID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_Currency Get(String CurrencyID)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select  top 1 * from [Sys_Currency] ");
            strSql.Append(" where [CurrencyID]=@CurrencyID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("CurrencyID", CurrencyID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_Currency Get(DataRow row)
        {
            if (row == null) return null;

            Sys_Currency model = new Sys_Currency();
	
            
            if (row["CurrencyID"] != DBNull.Value)
            {
                model.CurrencyID = (String)row["CurrencyID"];
            }       
            
            if (row["CurrencyName"] != DBNull.Value)
            {
                model.CurrencyName = (String)row["CurrencyName"];
            }      
            
            if (row["Rate"] != DBNull.Value)
            {
                model.Rate = (Decimal)row["Rate"];
            }      
            
            if (row["Memo"] != DBNull.Value)
            {
                model.Memo = (String)row["Memo"];
            }      
            
            if (row["Creator"] != DBNull.Value)
            {
                model.Creator = (String)row["Creator"];
            }      
            
            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }      
            
            if (row["LastModifier"] != DBNull.Value)
            {
                model.LastModifier = (String)row["LastModifier"];
            }      
            
            if (row["LastModifyTime"] != DBNull.Value)
            {
                model.LastModifyTime = (DateTime)row["LastModifyTime"];
            }      
            

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select * from [Sys_Currency] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Sys_Currency] ");
			
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if(filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
			
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Sys_Currency] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Sys_Currency]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by CurrencyID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
      #endregion  成员方法

        /// <summary>
        /// 获取所有币种
        /// </summary>
        /// <returns></returns>
        public DataTable GetCurrency()
        {
            string sql = string.Format(@" SELECT [CurrencyID],[CurrencyName],[Rate],[Memo],[Creator],[CreateTime],[LastModifier],[LastModifyTime] FROM Sys_Currency ORDER BY CreateTime DESC");
            return DbHelperSQL.Query(sql).Tables[0];
        }
    }
}


  